package dao.mysql;
import bean.Detalle_Comprobante_Pago;
import dao.Entidad;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import util.DataBaseConnection;

public class MySQLDetalle_Comprobante_PagoDAO implements Entidad<Detalle_Comprobante_Pago> {

	@Override
	public Detalle_Comprobante_Pago insert(Detalle_Comprobante_Pago t) {
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="INSERT INTO detalle_comprabante_pago(id_comp_pago, id_producto, monto_por_prod, cantidad";
			sql = sql + ") VALUES(?,?,?,?)";
			PreparedStatement ps=con.prepareStatement(sql);
					//prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setInt(1, t.getId_comp_pago());
			ps.setInt(2, t.getId_producto());
			ps.setDouble(3, t.getMonto_pro_prod());
			ps.setDouble(4, t.getCantidad());
			ps.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return t;
	}

	@Override
	public int update(Detalle_Comprobante_Pago t) {
		Connection con=null;
		int rows = -1;
		try {
			con=DataBaseConnection.openConnection();
			String sql="UPDATE detalle_comprabante_pago SET monto_por_prod=?, cantidad=?";
			sql = sql +	"WHERE id_comp_pago = ? AND id_producto=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setDouble(1, t.getMonto_pro_prod());
			ps.setDouble(2, t.getCantidad());
			ps.setInt(3, t.getId_comp_pago());
			ps.setInt(3, t.getId_producto());		
			rows =ps.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return rows;
	}

	@Override
	public int delete(Detalle_Comprobante_Pago t) {
		Connection con=null;
		int rows=-1;
		try {
			con=DataBaseConnection.openConnection();
			String sql="DELETE FROM detalle_comprabante_pago WHERE id_comp_pago = ? AND id_producto=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getId_comp_pago());
			ps.setInt(1, t.getId_producto());
			rows=ps.executeUpdate();
			System.out.println(rows);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return rows;
	}

	@Override
	public Detalle_Comprobante_Pago findById(Detalle_Comprobante_Pago t) {
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="SELECT * FROM detalle_comprabante_pago WHERE id_comp_pago=? and id_producto=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getId_comp_pago());
			ps.setInt(2, t.getId_producto());	
			ResultSet rs=ps.executeQuery();
			while(rs.next()){
				t.setId_comp_pago(rs.getInt("id_comp_pago"));
				t.setId_producto(rs.getInt("id_Producto"));
				t.setMonto_pro_prod(rs.getInt("monto_pro_prod"));
				t.setCantidad(rs.getInt("cantidad"));
						
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return t;
	}

	@Override
	public List<Detalle_Comprobante_Pago> listAll() {
		List<Detalle_Comprobante_Pago> detalle = new ArrayList<Detalle_Comprobante_Pago>();
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="SELECT * FROM detalle_comprabante_pago where  id_comp_pago=? and id_producto=?";
			Statement st=con.createStatement();
			ResultSet rs=st.executeQuery(sql);
			while(rs.next()){
				Detalle_Comprobante_Pago t = new Detalle_Comprobante_Pago();
				t.setId_comp_pago(rs.getInt("id_comp_pago"));
				t.setId_producto(rs.getInt("id_Producto"));
				t.setMonto_pro_prod(rs.getInt("monto_pro_prod"));
				t.setCantidad(rs.getInt("cantidad"));
				detalle.add(t);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return detalle;
	}

	@Override
	public List<Detalle_Comprobante_Pago> listAllCustom(Object... objects) {
		// TODO Auto-generated method stub
		return null;
	}

}
